package ml.coursedesign.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ml.coursedesign.common.lang.Result;
import ml.coursedesign.common.util.JWTUtils;
import ml.coursedesign.entity.Admin;
import ml.coursedesign.entity.Student;
import ml.coursedesign.entity.Teacher;
import ml.coursedesign.entity.User;
import ml.coursedesign.service.AdminService;
import ml.coursedesign.service.StudentService;
import ml.coursedesign.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class LoginController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentService studentService;
    @Autowired
    AdminService adminService;
    
    
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String token = "";
        
        if (!getStudentToken(user).equals("")) {
            
            token = getStudentToken(user);
        }
        if (!getTeacherToken(user).equals("")) {
            
            token = getTeacherToken(user);
        }
        if (!getAdminToken(user).equals("")) {
            
            token = getAdminToken(user);
        }
        
        return Result.succ(token);
    }
    
    @GetMapping("/userinfo")
    public Result getInfo(String token) {
        String name = new JWTUtils().getUsernameFromToken(token);
        System.out.println("本次请求Token：" + token);
        return Result.succ(name);
    }
    
    @GetMapping("/get/identity/{token}")
    public Result getIdentity(@PathVariable String token) {
        String role = new JWTUtils().getRoleFromToken(token);
        // System.out.println(role);
        return Result.succ(role);
    }
    @GetMapping("/get/username/{token}")
    public Result getUserName(@PathVariable String token){
        String username = new JWTUtils().getUsernameFromToken(token);
        // System.out.println(role);
        return Result.succ(username);

    }
    
    /**
     * 根据用户名生成token
     *
     * @param user
     * @return
     */
    private String getStudentToken(User user) {
        String token = "";
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        Student student = studentService.getOne(queryWrapper);
        if (student == null) {
            return "";
        }
        if (user.getPassword().equals(student.getPassword())) {
            Integer id = student.getId();
            
            token = new JWTUtils().createToken(user.getUsername(), "student", id);
            return token;
        }
        return token;
    }
    
    private String getTeacherToken(User user) {
        String token = "";
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        Teacher teacher = teacherService.getOne(queryWrapper);
        if (teacher == null) {
            return "";
        }
        if (user.getPassword().equals(teacher.getPassword())) {
            Integer id = teacher.getId();
            
            token = new JWTUtils().createToken(user.getUsername(), "teacher", id);
            
        }
        return token;
    }
    
    private String getAdminToken(User user) {
        String token = "";
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        Admin admin = adminService.getOne(queryWrapper);
        if (admin == null) {
            return "";
        }
        if (user.getPassword().equals(admin.getPassword())) {
            Integer id = admin.getId();
            token = new JWTUtils().createToken(user.getUsername(), "admin", id);
        }
        return token;
    }
}
